#include<cstdio>
using namespace std;
typedef unsigned long long ULL;
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int a[9];
		ULL k;
		for(int i=1;i<=8;++i)scanf("%d",&a[i]);
		scanf("%llu",&k);
		--k;
		for(int i=1;i<=8;++i)
			k+=(1LL<<a[i]);
		if(k==18446744073709551615)printf("18446744073709551616\n");
		else printf("%llu\n",k+1);
	}
	return 0;
}
